package com.whsr.readservice.dao;

import com.whsr.readservice.vo.Article;
import com.whsr.readservice.vo.Chapter;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ChapterMapper {

    @Insert("<script>" +
            "insert into chapter (ch_id, ch_name, " +
            " ch_vip, ch_index, book_id, ch_charge, " +
            " create_time, update_time)" +
            "values " +
            "<foreach collection =\"chapters\" item=\"chapter\" index= \"index\" separator =\",\">" +
            "(#{chapter.chId}, #{chapter.chName}," +
            " #{chapter.chVip}, #{chapter.chIndex}, #{chapter.bookId}, #{chapter.chCharge}," +
            " now(), now())" +
            "</foreach>" +
            "</script>")
    int insertChapters(@Param("chapters") List<Chapter> chapters);

    @Select("select " +
            " id as id, " +
            " ch_id as chId, " +
            " ch_name as chName, " +
            " ch_vip as chVip, " +
            " ch_index as chIndex, " +
            " book_id as bookId, " +
            " data_flag as dataFlag, " +
            " ch_charge as chCharge, " +
            " create_time as createTime, " +
            " update_time as updateTime " +
            "from chapter " +
            " where book_id = #{bookId}" +
            " order by ch_index")
    List<Chapter> selectChaptersByBookId(@Param("bookId") Integer bookId);

    @Delete("delete from chapter where book_id = #{bookId}")
    int deleteByBookId(@Param("bookId") Integer bookId);

    @Select("select " +
            " ch_id as chId, " +
            " ch_name as chName, " +
            " book_id as bookId, " +
            " ch_charge as chCharge " +
            "from chapter " +
            " where book_id = #{bookId}" +
            " order by ch_index")
    List<Chapter>  selectChaptersByApp(Integer bookId);

    @Select("SELECT DISTINCT t.`book_id` AS bookId,t.`ch_id` AS chId FROM chapter t WHERE NOT EXISTS (SELECT 1 FROM chapter_content c WHERE t.`ch_id`=c.`ch_id` AND t.`book_id` = c.`book_id`)")
    List<Chapter> selectChaptersByNotContent();


    @Select("SELECT" +
            " t.`book_id` AS bookId, " +
            " t.`ch_id` AS chId, " +
            " t.ch_charge as chCharge " +
            "FROM " +
            "chapter t, " +
            "chapter b " +
            "WHERE t.`book_id` = b.`book_id` " +
            " AND b.`book_id` = #{bookId} " +
            " AND b.`ch_id` = #{chId} " +
            " AND (t.`ch_index` = (b.`ch_index` + 1) " +
            " OR t.`ch_index` = (b.`ch_index` - 1) " +
            " OR t.`ch_id` = #{chId})")
    List<Chapter> selectPreNextChIdByByBookIdAndChId(@Param("bookId") Integer bookId, @Param("chId") Integer chId);

    @Select("SELECT" +
            " t.`book_id` AS bookId, " +
            " t.`ch_id` AS chId, " +
            " t.ch_charge as chCharge " +
            "FROM " +
            "chapter t, " +
            "chapter b " +
            "WHERE t.`book_id` = b.`book_id` " +
            "AND t.`ch_index` = (b.`ch_index` - 1) " +
            "AND b.`book_id` = #{bookId} " +
            "AND b.`ch_id` = #{chId}")
    Chapter selectPreChIdByByBookIdAndChId(@Param("bookId") Integer bookId, @Param("chId") Integer chId);

    @Select("select " +
            " a.id as id, " +
            " a.ch_id as chId, " +
            " a.ch_name as chName, " +
            " a.ch_vip as chVip, " +
            " a.ch_index as chIndex, " +
            " a.book_id as bookId, " +
            " a.data_flag as dataFlag, " +
            " a.ch_charge as chCharge, " +
            " a.create_time as createTime, " +
            " a.update_time as updateTime " +
            "from chapter a " +
            " where not exists (select 1 from chapter_content b " +
            " where a.book_id = b. book_id and a.ch_id = b.ch_id )" +
            " and a.book_id = #{bookId}" +
            " order by a.ch_index")
    List<Chapter> selectChaptersNoContentByBookId(@Param("bookId") Integer bookId);
}
